package core;


import java.io.*;

/**
 *	The static methods in this class allow text  
 *	to be written to OR read from a file.
 *
 *	@author  Ms Cianci
 *	@since	 November 2006
 */
public class IO
{
	
	/* VARIABLE AND METHODS NEEDED FOR WRITING TO A FILE */
	
	private static PrintWriter fileOut;
	
	
	/**
	 * Creates a new file (fileName) in the current
	 * folder and places a reference to it in fileOut
	 * @param fileName Represents the name of the file
	 * @throws IOException 
	 */	
	public static void createOutputFile(String fileName) throws IOException
	{
			fileOut = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
	}
	
	
	/**
	 * Text is added to the current file
	 * @param text The characters that will be added to the file
	 */
	public static void print(String text)
	{
		fileOut.print(text);
	}


	/**
	 * Text is added to the current file and a new line
	 * is inserted at the end of the characters
	 * @param text The characters that will be added to the file
	 */
	public static void println(String text)
	{
		fileOut.println(text);
	}

	
	/**
	 * Close the file that is currently being written to
	 * NOTE: This method MUST be called when you are finished
	 *		 writing to a file in order to have your changes saved
	 */
	public static void closeOutputFile()
	{
		fileOut.close();
	}
	
	
	/* VARIABLE AND METHODS NEEDED FOR READING FROM A FILE */
	
	private static BufferedReader fileIn;
	
	
	/**
	 * Opens a file called fileName (that must be
	 * stored in the current folder) and places a
	 * reference to it in fileIn
	 * @param fileName The name of a file that already exists
	 */
	public static void openInputFile(String fileName) throws FileNotFoundException
	{
			fileIn = new BufferedReader(new FileReader(fileName));
		
	}
	
	
	/**
	 * Read the next line from the file and return it
	 */
	public static String readLine() throws IOException
	{
		return fileIn.readLine();
	}
	
	
	/**
	 * Close the file that is currently being read from
	 */
	public static void closeInputFile() throws IOException
	{
		fileIn.close();
	}
	
} // end class